﻿$(document).ready(function (event) 
{
    /* google map settings*/
    $('#Address_PostCode').keyup(function (e)
     {
        var postcode = $('#Address_PostCode').val();
        $("#imgMap").attr("src", "http://maps.googleapis.com/maps/api/staticmap?center=" + postcode + "&zoom=14&size=300x300&maptype=roadmap&sensor=false&markers=color:green%7Clabel:P%7C" + postcode);
     });

    // Set initial picture
    var postcode = $('#Address_PostCode').val();
    $("#imgMap").attr("src", "http://maps.googleapis.com/maps/api/staticmap?center=" + postcode + "&zoom=14&size=300x300&maptype=roadmap&sensor=false&markers=color:green%7Clabel:P%7C" + postcode);
   

    /*  File upload complete function  */

    var fileIndex = 0;

    function onFail(e, data) 
    {
        $('#fileError').html(" Please select .png, .jpeg, .jpg or .gif image with size less than 2MB.");
        $('#fileProgress').hide();
        $('#file_Error').show();
    }

    function onFileUploaded(e, data) 
    {
        $(data.result).each(function (index)
           {
            $('#fileProgress').hide();
            var file = data.result[index];
            if (file.Error == "OK")
             {

                 var img = $('<img src="/ListingPicture/' + file.ThumbnailUrl + '" class="imgupload" />');
                var rmBtn = $('<input type="button" value="Remove" tag ="' + fileIndex + '"  imageid="' + file.ThumbnailUrl + '" class="ui-state-hover" />').click(function () 
                {
                    $('#divFile' + $(this).attr('tag')).remove();
                    $('#FileList').val($('#FileList').val().replace(',' + $(this).attr('imageid'), ''));
                });
                var div = $('<div id="divFile' + fileIndex++ + '" />').addClass('imguploadedbox').append(img);
                $(div).append(rmBtn);

                $('#fileResult').append($(div));
                $('#FileList').val($('#FileList').val() + ',' + file.ThumbnailUrl);
                $('#file_Error').hide();
            }
            else {
                $('#fileError').html(file.Error);
                $('#file_Error').show();
            }
        });
    }

    /*  File Upload */

    $("#fileupload").fileinput({ buttonText: "+ Add" });

    $('#file_Error').hide();

    $('#fileupload').fileupload({
        dataType: 'json',
        url: "/Upload/edit",
        replaceFileInput: false,
        done: onFileUploaded,
        fail: onFail,
        start: function (e) {
            $('#fileProgress').show();
        }
    });

    var fileList = $('#FileList').val();

    fileList = fileList.split(',');

    $(fileList).each(function (index) {

        if (fileList[index].length > 1) {
            var img = $('<img src="/ListingPicture/' + fileList[index] + '" class="imgupload" />');
            var rmBtn = $('<input type="button" value="Remove" tag ="' + fileIndex + '" class="ui-state-hover" />').click(function () {
                $('#divFile' + $(this).attr('tag')).remove();
                $('#FileList').val($('#FileList').val().replace(',' + fileList[index], ''));
            });
            var div = $('<div id="divFile' + fileIndex++ + '" />').addClass('imguploadedbox').append(img);
            $(div).append(rmBtn);

            $('#fileResult').append($(div));

        }
    });

    /* Price Listing */
    var PriceJson = Array();
    var tempPrice = $.parseJSON($('#PriceListFinal').val());
	
    if (tempPrice != null) {
        PriceJson = tempPrice;
    }

    var tempHtml = '';
    $(tempPrice).each(function (index) {
        tempHtml += '<div style="float:left; width:200px;">' + tempPrice[index].Vehicle + '      £ ' + tempPrice[index].Price + ' / ' + tempPrice[index].PriceDuration + ' </div><br/>';
    });

    $('#PriceList').html(tempHtml);

    $('#AddPrice').click(function () {

        $('#Price_Error').hide();
        var Price = $('#Price').val();
        var PriceDuration = $('#Duration').val();
        var Vehicle = $('#Vehicle').val();

        if (Price == null || Price != parseFloat(Price)) {
            $('#Price_Error').show();
            return false;
        }

        Price = parseFloat(Price);
        if (Price == 0) {
            $('#Price_Error').show();
            return false;
        }

        /* Price List logic  */
        //Check for duplicate and update
        var isDup = false;
        $(PriceJson).each(function (index) {

            if (PriceJson[index].PriceDuration == PriceDuration && PriceJson[index].Vehicle == Vehicle) {
                PriceJson[index].Price = Price;
                isDup = true;
            }

        });

        //Add new price
        if (!isDup)
            PriceJson[PriceJson.length] = { "Price": Price, "PriceDuration": PriceDuration, "Vehicle": Vehicle };

        var newHtml = '';
        var priceString = '';
        $(PriceJson).each(function (index) {

            newHtml += '<div style="float:left; width:200px;">' + PriceJson[index].Vehicle + '      £ ' + PriceJson[index].Price + ' / ' + PriceJson[index].PriceDuration + ' </div><br/>';

            priceString += ',{"PriceDuration" : "' + PriceJson[index].PriceDuration + '", "Vehicle" : "' + PriceJson[index].Vehicle + '", "Price" : "' + PriceJson[index].Price + '" }';

        });

        $('#PriceList').html(newHtml);

        priceString = '[' + priceString.substring(1, priceString.length) + ']';

        $('#PriceListFinal').val(priceString);

    });
});    